#include <iostream>
using namespace std;

// 商店
struct store
{
	int x;
	int y;
};

// 商店数量
int n;

// 商店数组
store A[10];

// 记录商店围成的最小矩形
int xmin = 0x7F, xmax = 0, ymin = 0x7F, ymax = 0;

// 贪心，横向纵向都找距离最短的
int xsum = 0x7F, ysum = 0x7F;

store best_pos()
{
	int bx, by; // best_x, best_y
	int i, j;
	// 求商店围成的最小矩形
	for (i = 0; i < n; i++)
	{
		if (A[i].x < xmin)
			xmin = A[i].x;
		else if (A[i].x > xmax)
			xmax = A[i].x;

		if (A[i].y < ymin)
			ymin = A[i].y;
		else if (A[i].x > ymax)
			ymax = A[i].y;
	}

	// 设置仓库位置变量
	for (i = xmin; i <= xmax; i++)
	{
		int sum = 0;
		for (j = 0; j < n; j++)
			sum += abs(A[j].x - i);
		if (sum < xsum)
		{
			// 如果当前横轴距离各商店横向距离总和比上一个横轴小则替换
			xsum = sum;
			bx = i;
		}
	}
	for (i = ymin; i <= ymax; i++)
	{
		int sum = 0;
		for (j = 0; j < n; j++)
			sum += abs(A[j].y - i);
		if (sum < ysum)
		{
			// 如果当前纵轴距离各商店纵向距离总和比上一个纵轴小则替换
			ysum = sum;
			by = i;
		}
	}

	return {bx, by};
}

int main()
{
	system("chcp 65001");

	cout << "请输入商店数目：";
	cin >> n;
	cout << "请输入" << n << "个商店坐标：" << endl;
	for (int i = 0; i < n; i++)
	{
		cin >> A[i].x >> A[i].y;
	}

	store warehouse = best_pos();
	cout << "最佳仓库位置：" << warehouse.x << ','
		 << warehouse.y << endl
		 << "总路程:" << xsum + ysum << endl;

	return 0;
}
